package com.example.mouther_bg.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mouther_bg.Bean.Video;
import com.example.mouther_bg.Mapper.VideoMapper;
import com.example.mouther_bg.service.videoService;
import com.example.mouther_bg.utils.OssUtil;
import com.example.mouther_bg.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Api(tags = "首页模块")
@CrossOrigin
@RestController
public class fileController {
    @Autowired
    private videoService videoService;
    @Autowired
    private VideoMapper videoMapper;
//    上传视频封面

    @RequestMapping(value = "/file/upload",method = RequestMethod.POST)
    public String uploadFile( @RequestParam MultipartFile multipartFile) throws IOException {
//        1:调用ossutil的upload
        if (multipartFile.getSize() > 0) {
            return OssUtil.upload(multipartFile.getOriginalFilename(), multipartFile.getInputStream());
        }
        return "";
    }

//    视频上传
    @ApiImplicitParam(name = "",value = "查询所有无需参数",required = true)
    @ApiOperation(value = "欢迎您")
//    @PostMapping("/voide")
//    public Result updateVideo(MultipartFile file) throws IOException {
//        String videoId = videoService.updateVideo(file);
//        if (videoId != null){
//            return Result.succ(videoId);
//        }
//        return Result.err();
//    }
//    发布视频
    @PutMapping("/insert/video")
    public Result insertVideo(@RequestBody Video video){
            video.setShowTime(new Date());
            videoService.save(video);
            return Result.succ(video);
    }

//    查询所有视频
    @GetMapping("/queryAll/video")
    public Result queryAll(){
        List<Video> list = videoService.list();
        return Result.succ(list);
    }
//    根据id查询相关视频
    @CrossOrigin
    @GetMapping("/playVideo")
    public Result queryVideosById(int id){
        Video video = videoService.getOneById(id);
        return Result.succ(video);
    }
//    删除视频
    @CrossOrigin
    @DeleteMapping("/delVideo")
    public Result deleteVideo(int id){
        boolean b = videoService.removeById(id);
        if (b){
            return Result.succ("");
        }
        return Result.err();
    }

//    根据视频关键字搜索视频

    @CrossOrigin
    @PostMapping("/queryKeyVideo")
    public Result queryKeysVideo(String videoContent){
        List<Video> videos = videoService.queryKeyVideo(videoContent);
        return Result.succ(videos);
    }

//查询所有视频
    @CrossOrigin
    @GetMapping("/queryAll")
    public Result queryVideoAll(){
        List<Video> videos = videoService.listAll();
        return Result.succ(videos);
    }
    //根据用户的id查询用户所对应的视频
    @CrossOrigin
    @GetMapping("/queryVideoByUserId")
    public Result queryVideoByUserId(int id){
        List<Video> videos = videoService.queryVideoByUserId(id);
        if (videos != null){
           return Result.succ(videos);
        }
        return Result.err();
    }
//    分页查询
    @CrossOrigin
    @GetMapping("/video/pageVideo")
    public Result pageQueryVideo(Integer page,Integer perPage){
        List<Video> videos = videoService.pageQuery(page*perPage, perPage);
        return Result.succ(videos);
    }
//    返回总记录条数
    @CrossOrigin
    @GetMapping("/video/pageVideocount")
    public Result pageVideocount(){
        int i = videoService.queryAllCount();
        return Result.succ(i);
    }
}
